package com.smyx;

import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;

import java.util.concurrent.TimeUnit;

public class TestCaffeine {
    /** 普通cache使用 */
    private static final Cache<String, Object> normalCache = Caffeine.newBuilder().build();
    /** 带过期时间 */
    private static final Cache<String, Object> timeCache = Caffeine.newBuilder()
            // 10s后过期
            .expireAfterWrite(10, TimeUnit.SECONDS)
            // 最后一次读取后多少秒直接过期
            .expireAfterAccess(5, TimeUnit.SECONDS)
            .build();

    public static void main(String[] args) throws InterruptedException {
        normalCache.put("normal-key-1", "n1");
        Object o = normalCache.getIfPresent("normal-key-1");
        System.out.println(o);

        timeCache.put("time-key-1", "t1");
        System.out.println(timeCache.getIfPresent("time-key-1"));
        Thread.sleep(4000);
        System.out.println(timeCache.getIfPresent("time-key-1"));
    }
}
